package xyz.syyrjx.blog.util.ai;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xyz.syyrjx.blog.dao.ai.AiIdentityDao;
import xyz.syyrjx.blog.entity.ai.AiIdentity;
import xyz.syyrjx.blog.exception.AiException;
import xyz.syyrjx.blog.util.CachePool;
import xyz.syyrjx.blog.util.ConstValuePool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 大地崩坏苍蝇兽
 * @date 2024/2/21 20:39
 * @description ...
 * @since v0.0.1
 */
public class AiIdentityFlagUtil {

    private static final Logger logger = LoggerFactory.getLogger(AiIdentityFlagUtil.class);

    public static AiIdentityDao aiIdentityDao;

    public static String checkAiIdentity(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            throw new AiException("无法获取到身份id载体，请刷新重试");
        }
        for (Cookie cookie : cookies) {
            if (ConstValuePool.AI_IDENTITY_FLAG.equals(cookie.getName())) {
                String value = cookie.getValue();
                // 检验是否右这个身份标识
                AiIdentity template = new AiIdentity();
                template.setIdentity(value);
                AiIdentity result = aiIdentityDao.selectAiIdentity(template);
                // 这个身份标识是由系统发送的，表示用户是由页面进入的
                if (result != null) {
                    logger.debug("{}",result);
                    // 即将过期，就延长有效期
                    if (System.currentTimeMillis()
                            - result.getCreateTime().getTime()
                            - ConstValuePool.COOKIE_MAX_AGE_MILLIS < ConstValuePool.COOKIE_ABOUT_TO_EXPIRE_LIMIT) {
                        cookie.setMaxAge(cookie.getMaxAge() + ConstValuePool.COOKIE_MAX_AGE);
                        aiIdentityDao.flushCookie(result.getId());
                        response.addCookie(cookie);
                    }
                    return value;
                }
                throw new AiException("身份信息有误");
            }
        }
        throw new AiException("没有查询到身份信息");
    }

    public static String getAiIdentity(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null) {
            return null;
        }
        for (Cookie cookie : cookies) {
            if (ConstValuePool.AI_IDENTITY_FLAG.equals(cookie.getName())) {
                String value = cookie.getValue();
                // 检验缓存是否右这个身份标识
                if (CachePool.AI_IDENTITY_SEND_BY_SYSTEM.contains(value)){
                    return value;
                }
                AiIdentity template = new AiIdentity();
                // 检查数据库是否有这个身份标识
                template.setIdentity(value);
                AiIdentity result = aiIdentityDao.selectAiIdentity(template);
                // 这个身份标识是由系统发送的，表示用户是由页面进入的
                if (result != null) {
                    // 缓存
                    CachePool.AI_IDENTITY_SEND_BY_SYSTEM.add(value);
                    return value;
                }
                break;
            }
        }
        return null;
    }

}
